package lib;

/**
 * Provides static helper methods for argument validations
 * @author abhip
 *
 */
public final class ArgumentValidator
{
    /**
     * Checks if argument equals other argument
     * @param argName Name of the argument
     * @param arg Argument
     * @param other object to compare against
     */
    public static <T extends Comparable<? super T>> void AreEqual(
            String argName,
            T arg,
            T other)
            throws IllegalArgumentException
    {
        if (! arg.equals(other))
        {
            throw new IllegalArgumentException(
                    String.format(
                            "Argument '%s' with value %s is supposed to be equal to %s!",
                            argName,
                            arg,
                            other));
        }
    }

    /**
     * Checks if argument is greater than min and less than max both inclusive
     * @param argName Name of the argument
     * @param arg Argument
     * @param min Value to compare against
     * @param max Value to compare against
     */
    public static <T extends Comparable<? super T>> void IsBetweenBothInclusive(
            String argName,
            T arg,
            T min,
            T max)
            throws IllegalArgumentException
    {
        if (arg.compareTo(min) < 0)
        {
            throw new IllegalArgumentException(
                    String.format(
                            "Argument '%s' with value %s is supposed to be between %s and %s!",
                            argName,
                            arg,
                            min,
                            max));
        }

        if (arg.compareTo(max) > 0)
        {
            throw new IllegalArgumentException(
                    String.format(
                            "Argument '%s' with value %s is supposed to be between %s and %s!",
                            argName,
                            arg,
                            min,
                            max));
        }
    }

    /**
     * Checks if argument is greater than min
     * @param argName Name of the argument
     * @param arg Argument
     * @param min Value to compare against
     */
    public static <T extends Comparable<? super T>> void IsGreaterThan(
            String argName,
            T arg,
            T min)
            throws IllegalArgumentException
    {
        if (arg.compareTo(min) <= 0)
        {
            throw new IllegalArgumentException(
                    String.format(
                            "Argument '%s' with value %s is supposed to be greater than %s!",
                            argName,
                            arg,
                            min));
        }
    }

    /**
     * Checks if argument is greater than or equal to min
     * @param argName Name of the argument
     * @param arg Argument
     * @param min Value to compare against
     */
    public static <T extends Comparable<? super T>> void IsGreaterThanOrEqualTo(
            String argName,
            T arg,
            T min)
            throws IllegalArgumentException
    {
        if (arg.compareTo(min) < 0)
        {
            throw new IllegalArgumentException(
                    String.format(
                            "Argument '%s' with value %s is supposed to be greater than or equal to %s!",
                            argName,
                            arg,
                            min));
        }
    }

    /**
     * Checks if argument is less than max
     * @param argName Name of the argument
     * @param arg Argument
     * @param max Value to compare against
     */
    public static <T extends Comparable<? super T>> void IsLessThan(
            String argName,
            T arg,
            T max)
            throws IllegalArgumentException
    {
        if (arg.compareTo(max) >= 0)
        {
            throw new IllegalArgumentException(
                    String.format(
                            "Argument '%s' with value %s is supposed to be less than %s!",
                            argName,
                            arg,
                            max));
        }
    }

    /**
     * Checks if argument is less than or equal to max
     * @param argName Name of the argument
     * @param arg Argument
     * @param max Value to compare against
     */
    public static <T extends Comparable<? super T>> void IsLessThanOrEqualTo(
            String argName,
            T arg,
            T max)
            throws IllegalArgumentException
    {
        if (arg.compareTo(max) > 0)
        {
            throw new IllegalArgumentException(
                    String.format(
                            "Argument '%s' with value %s is supposed to be less than or equal to %s!",
                            argName,
                            arg,
                            max));
        }
    }

    /**
     * Checks if argument is null
     * @param argName Name of the argument
     * @param arg Argument
     */
    public static void IsNotNull(String argName, Object obj)
        throws IllegalArgumentException
    {
        if (obj == null)
        {
            throw new IllegalArgumentException(
                    String.format(
                            "Argument '%s' is not supposed to be null!",
                            argName));
        }
    }

    /**
     * Checks if given string is null or empty
     * @param argName Name of the argument
     * @param arg String argument
     */
    public static void IsNotNullOrEmpty(String argName, String arg)
            throws IllegalArgumentException
        {
            if (arg == null)
            {
                throw new IllegalArgumentException(
                        String.format(
                                "Argument '%s' is not supposed to be null!",
                                argName));
            }
            else if (arg.length() == 0)
            {
                throw new IllegalArgumentException(
                        String.format(
                                "Argument '%s' is not supposed to be empty!",
                                argName));
            }
        }
}
